//
//  SLLoginViewController.m
//  SoccerLottery
//
//  Created by chuanshuangzhang chuan shuang on 15/12/24.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "SLLoginViewController.h"
#import "SLForgetPassCodeViewController.h"
#import "SLRegisterViewController.h"

@interface SLLoginViewController () <UITextFieldDelegate,SLRegisterViewControllerDelegate>

@property (nonatomic,strong) UITextField *accountField;
@property (nonatomic,strong) UITextField *passcodeField;

@end

@implementation SLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"会员登陆" back:YES];
    [self addKeyboardButton];
    //设置输入框的视图
    UIView *inputContentView = [[UIView alloc]init];
    inputContentView.frame = CGRectMake(15, 50, App_Frame_Width-30, 88);
    inputContentView.layer.masksToBounds = YES;
    inputContentView.layer.borderWidth = 0.5;
    inputContentView.layer.borderColor = RGBACOLOR(151, 151, 151, 1.0).CGColor;
    inputContentView.layer.cornerRadius = 5.0;
    [self.view addSubview:inputContentView];
    //设置中间的分割线
    UIView *lineView = [[UIView alloc]init];
    lineView.frame = CGRectMake(0, inputContentView.height*0.5, inputContentView.width, 0.5);
    lineView.backgroundColor = RGBACOLOR(151, 151, 151, 1.0);
    [inputContentView addSubview:lineView];
    
    //设置账号输入框
    _accountField = [[UITextField alloc]init];
    _accountField.frame = CGRectMake(0, 0, inputContentView.width, inputContentView.height/2.0);
    _accountField.delegate = self;
    _accountField.keyboardType = UIKeyboardTypePhonePad;
    [_accountField setPlaceholder:@"手机号"];
    [self setInputTextFieldStyle:_accountField];
    [inputContentView addSubview:_accountField];
    
    //设置密码输入框
    _passcodeField = [[UITextField alloc]init];
    [_passcodeField setPlaceholder:@"密码"];
    [self setInputTextFieldStyle:_passcodeField];
    _passcodeField.frame = CGRectMake(0, inputContentView.height/2.0, inputContentView.width, inputContentView.height/2.0);
    _passcodeField.delegate = self;
    _passcodeField.secureTextEntry = YES;
    [inputContentView addSubview:_passcodeField];
    
    //忘记密码按钮
    UIButton *forgotCodeBtn = [[UIButton alloc]init];
    forgotCodeBtn.frame = CGRectMake(inputContentView.origin.x+15, inputContentView.bottom + 15, 100, 30);
    [forgotCodeBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgotCodeBtn setTitleColor:RGBACOLOR(236, 73,93, 1.0) forState:UIControlStateNormal];
    [forgotCodeBtn setTitleColor:RGBACOLOR(153, 153,153, 1.0) forState:UIControlStateHighlighted];
    [forgotCodeBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [forgotCodeBtn addTarget:self action:@selector(forgetPasscode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgotCodeBtn];
    
    // 设置登陆按钮
    UIButton *loginBtn = [[UIButton alloc]init];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.frame = CGRectMake(25, forgotCodeBtn.bottom + 30, App_Frame_Width-50, 44);
    [loginBtn applyActionStyle];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];

    // 设置注册按钮
    UIButton *registerBtn = [[UIButton alloc]init];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    registerBtn.frame = CGRectMake(25, loginBtn.bottom + 20, App_Frame_Width-50, 44);
    [registerBtn setBackgroundImage:[Utils createImageWithColor:RGBACOLOR(152, 153, 155, 1.0)] forState:UIControlStateNormal];
    [registerBtn setBackgroundImage:[Utils createImageWithColor:RGBACOLOR(239, 91, 110, 1.0)] forState:UIControlStateHighlighted];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [registerBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [registerBtn.layer setMasksToBounds:YES];
    registerBtn.layer.cornerRadius = 3.0;
    [registerBtn addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}

- (void)setInputTextFieldStyle:(UITextField *)textField
{
    textField.leftViewMode = UITextFieldViewModeAlways;
    UIView *leftView = [[UIView alloc]init];
    leftView.frame = CGRectMake(0, 0, 20, 44);
    textField.leftView = leftView;
    textField.textColor = RGBACOLOR(151, 151, 151, 1.0);
    textField.font = [UIFont systemFontOfSize:16];
}

- (IBAction)forgetPasscode:(id)sender
{
    [self resignKeyboard:nil];
    SLForgetPassCodeViewController *VC = [[SLForgetPassCodeViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (IBAction)login:(id)sender
{
     [self resignKeyboard:nil];
    if(self.accountField.text.length != 11){
        [self.accountField becomeFirstResponder];
        return [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
    }else if(self.passcodeField.text.length <=0){
        [self.passcodeField becomeFirstResponder];
        return [SVProgressHUD showErrorWithStatus:@"请输入密码"];
    }else if(![VWebService isConnected]){
        return [SVProgressHUD showErrorWithStatus:@"请检查您的网络链接"];
    }
    [SVProgressHUD showWithStatus:@"正在登录"];
    [SLUserNetMgr login:self.accountField.text password:self.passcodeField.text block:^(id result, BOOL status, NSError *error) {
        if(NET_SUCCESS(result, status)){
            [SVProgressHUD dismiss];
            NSDictionary *dic = result[@"result"];
            [UserModel sharedInstance].userId = CS(dic[@"UserID"]);
            [UserModel sharedInstance].phone = CS(dic[@"Telephone"]);
            [UserModel sharedInstance].password = CS(dic[@"Password"]);
            [UserModel sharedInstance].userName = CS(dic[@"UserName"]);
            
            NSInteger coins = [CS(dic[@"Points"]) integerValue];
            NSString *localCoins = [[UserModel sharedInstance] getLocalCoins];
            [[UserModel sharedInstance] clearLocalCoins];
            if([localCoins integerValue] > 0){
               [SLNewNetMgr setCoins:[UserModel sharedInstance].userId coins:localCoins recommandId:@"" block:^(id result, BOOL status, NSError *error) {
                   
               }];
            }
            [UserModel sharedInstance].coin = [NSString stringWithFormat:@"%ld",coins + [localCoins integerValue]];
            [SLDBManager saveUserAccount:[UserModel sharedInstance].phone passcode:[UserModel sharedInstance].password];
            [self.navigationController popViewControllerAnimated:YES];
            [SLUserNetMgr setVisit:nil];
        }else {
            [SVProgressHUD showErrorWithStatus:ERRORMSG(result)];
        }
    }];
}
- (IBAction)registerUser:(id)sender
{
     [self resignKeyboard:nil];
    SLRegisterViewController *VC = [[SLRegisterViewController alloc]init];
    VC.delegate = self;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)didUserRegisterSucced:(SLRegisterViewController *)registerVc
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
